package com.agrishare.service;

import com.agrishare.entity.Post;
import com.agrishare.entity.Category;
import com.agrishare.entity.User;
import com.agrishare.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Transactional
public class PostService {
    
    @Autowired
    private PostRepository postRepository;
    
    @Autowired
    private UserService userService;
    
    public Post createPost(Post post) {
        Post savedPost = postRepository.save(post);
        // 发帖奖励积分
        userService.addPoints(post.getAuthor().getId(), 10);
        return savedPost;
    }
    
    public Optional<Post> findById(Long id) {
        return postRepository.findById(id);
    }
    
    public Post updatePost(Post post) {
        return postRepository.save(post);
    }
    
    public void deletePost(Long id) {
        postRepository.deleteById(id);
    }
    
    public Page<Post> findApprovedPosts(Pageable pageable) {
        return postRepository.findByIsApprovedTrueOrderByCreatedAtDesc(pageable);
    }
    
    public Page<Post> findPostsByCategory(Category category, Pageable pageable) {
        return postRepository.findByCategoryAndIsApprovedTrueOrderByCreatedAtDesc(category, pageable);
    }
    
    public Page<Post> findPostsByAuthor(User author, Pageable pageable) {
        return postRepository.findByAuthorOrderByCreatedAtDesc(author, pageable);
    }
    
    public Page<Post> searchPosts(String keyword, Pageable pageable) {
        return postRepository.findByKeyword(keyword, pageable);
    }
    
    public Page<Post> findPendingPosts(Pageable pageable) {
        return postRepository.findPendingPosts(pageable);
    }
    
    public Page<Post> findPinnedPosts(Pageable pageable) {
        return postRepository.findPinnedPosts(pageable);
    }
    
    public Page<Post> findFeaturedPosts(Pageable pageable) {
        return postRepository.findFeaturedPosts(pageable);
    }
    
    public void approvePost(Long postId) {
        Optional<Post> postOpt = postRepository.findById(postId);
        if (postOpt.isPresent()) {
            Post post = postOpt.get();
            post.setIsApproved(true);
            postRepository.save(post);
            // 审核通过奖励积分
            userService.addPoints(post.getAuthor().getId(), 5);
        }
    }
    
    public void pinPost(Long postId) {
        Optional<Post> postOpt = postRepository.findById(postId);
        if (postOpt.isPresent()) {
            Post post = postOpt.get();
            post.setIsPinned(true);
            postRepository.save(post);
        }
    }
    
    public void unpinPost(Long postId) {
        Optional<Post> postOpt = postRepository.findById(postId);
        if (postOpt.isPresent()) {
            Post post = postOpt.get();
            post.setIsPinned(false);
            postRepository.save(post);
        }
    }
    
    public void featurePost(Long postId) {
        Optional<Post> postOpt = postRepository.findById(postId);
        if (postOpt.isPresent()) {
            Post post = postOpt.get();
            post.setIsFeatured(true);
            postRepository.save(post);
            // 加精奖励积分
            userService.addPoints(post.getAuthor().getId(), 20);
        }
    }
    
    public void incrementViewCount(Long postId) {
        Optional<Post> postOpt = postRepository.findById(postId);
        if (postOpt.isPresent()) {
            Post post = postOpt.get();
            post.setViewCount(post.getViewCount() + 1);
            postRepository.save(post);
        }
    }
}